import React, { useEffect, useState } from 'react';
import { Button, Flex, Toast } from '@ant-design/react-native';
import UserStore from '@/stores/userStore';
import LmoNrcStore from './lmonrcStore';
import LmoCardView from './lmoCard';

// LMO 审批页面
function LmoAuditPage({ route, navigation }) {
  const [lmoData, setLmoData] = useState({});
  const userStore = UserStore.useStore();
  const lmonrcStore = LmoNrcStore.useStore();
  useEffect(() => {
    if (route.params) {
      handleLoad(route.params);
    }

  }, [route.params]);
  const handleLoad = async (lmoItem) => {
    setLmoData(lmoItem);
    // const res = await lmonrcStore.getLmoData(id);
    // if (!res.errorCode) {
    //   setLmoData(res.data);
    // }
  };
  const handleLmoChange = (field, value) => {
    setLmoData({ ...lmoData, [field]: value });
  };
  const handleReject = async () => {
    const res = await lmonrcStore.rejectLmo(userStore.user.LoginName, lmoData);
    if (!res.errorCode) {
      navigation.goBack();
    } else {
      Toast.fail(res.errorMsg);
    }
  };
  const handleAccept = async () => {
    const res = await lmonrcStore.acceptLmo(userStore.user.LoginName, lmoData);
    if (!res.errorCode) {
      navigation.goBack();
    } else {
      Toast.fail(res.errorMsg);
    }
  };
  return (
    <>
      <LmoCardView
        lmo={lmoData}
        onFieldChange={handleLmoChange}
        withAudit={true}
        inAudit={true}
      />
      <Flex>
        <Flex.Item>
          <Button
            type="primary"
            onPress={handleAccept}
            loading={lmonrcStore.acceptLmo.loading}
          >
            同意
          </Button>
        </Flex.Item>
        <Flex.Item>
          <Button
            type="ghost"
            onPress={handleReject}
            loading={lmonrcStore.rejectLmo.loading}
          >
            退回
          </Button>
        </Flex.Item>
      </Flex>
    </>
  );
}

LmoAuditPage.routeName = 'LmoAuditPage';
LmoAuditPage.permission = 'RLmoAudit';
export default LmoAuditPage;
